﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using rd = global::Reviewer.Data;
using Reviewer.Entities;
using MySql.Data.MySqlClient;

namespace Test.Reviewer.Data
{
    /// <summary>
    /// Summary description for TableDataTest
    /// </summary>
    [TestClass]
    public class TableDataTest
    {
        public TableDataTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestMethod1()
        {
            rd.GameRepository repo = new rd.GameRepository();
            Game newGame = new Game();
            newGame.GameId = Guid.NewGuid();
            newGame.Title = "Test Game " + DateTime.Now.Ticks.ToString();
            newGame.Publisher = "Test publisher" + DateTime.Now.Ticks.ToString();
            newGame.Developer = "Test developer" + DateTime.Now.Ticks.ToString();
            newGame.Genre = 0;
            newGame.MultiplayerInfo = "Test Multi" + DateTime.Now.Ticks.ToString();
            newGame.Format = 0;
            newGame.ReleaseDate = DateTime.Now;
            newGame.UpdateDate = DateTime.Now;

            Game savedGame = repo.Create(newGame);

            Assert.IsNotNull(savedGame.Id);
            Assert.IsTrue(savedGame.Id > 0);
            Assert.AreEqual(newGame.GameId, savedGame.GameId);
            Assert.AreEqual(newGame.Title, savedGame.Title);
            Assert.AreEqual(newGame.Publisher, savedGame.Publisher);
            Assert.AreEqual(newGame.Developer, savedGame.Developer);
            Assert.AreEqual(newGame.Genre, savedGame.Genre);
            Assert.AreEqual(newGame.MultiplayerInfo, savedGame.MultiplayerInfo);
            Assert.AreEqual(newGame.Format, savedGame.Format);
            Assert.AreEqual(newGame.ReleaseDate, savedGame.ReleaseDate);
            Assert.AreEqual(newGame.UpdateDate, savedGame.UpdateDate);
        }

        [TestMethod]
        public void Queryable()
        {
            /*
            var provider = new IQToolkit.Data.MySqlClient.MySqlQueryProvider(
                new MySql.Data.MySqlClient.MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Reviewer.Data"].ConnectionString),
                new IQToolkit.Data.Mapping.AttributeMapping(typeof(ContextItem)),
                IQToolkit.Data.Common.QueryPolicy.Default);
            //DbEntityProvider.From(
            //"IQToolkit.Data.MySqlClient",
            //System.Configuration.ConfigurationManager.ConnectionStrings["Reviewer.Data"].ConnectionString,
            //"MembersAttribute");

            var db = new ContextItem(provider);

            var currentUser = (from m in db.Members where m.UserName == "Steffen Balslev" select m).FirstOrDefault();
            if (currentUser != null)
            {
                var openIdValue = (from m in db.MembersAttribute where m.MembersId == currentUser.Id && m.Key == "OpenId" select m).FirstOrDefault();
                if (null != openIdValue)
                {
                    Console.WriteLine("OpenId value = " + openIdValue.Value);
                }
                else
                    Console.WriteLine("Not found");
            }
            else
            {
                Console.WriteLine("User not found");
            }

            /*
            var matches = (from m in db.MembersAttribute select m).ToList();

            foreach (var m in matches)
            {
                Console.WriteLine(m.Key + " = " + m.Value);
            }

            MembersAttribute attr = new MembersAttribute();
            attr.MembersId = new Guid("6f2762de-da77-102c-bcca-f36e4e3e49b5");
            attr.Key = "MyKey";
            attr.Value = "MyValue";
            db.MembersAttribute.InsertOrUpdate(attr);

            var matches2 = (from m in db.MembersAttribute where m.Key == "Open Id" select m).ToList();
            Console.WriteLine(matches2.Count);
             * */

            
        }
        /*
        public class MembersAttribute
        {
            public Guid MembersId { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
        }
        public class Members
        {
            public Guid Id { get; set; }
            public string UserName { get; set; }
        }

        public class ContextItem
        {
            public ContextItem(IEntityProvider provider)
            {
                this.EntityProvider = provider;
            }

            public IEntityProvider EntityProvider
            {
                get;
                set;
            }

            [Table(Name = "membersattribute", Alias = "MA")]
            [Column(Member = "MembersId", Name = "MembersId", IsPrimaryKey = true, IsGenerated = false)]
            [Column(Member = "Key", Name = "KeyName", IsPrimaryKey = true, IsGenerated = false)]
            [Column(Member = "Value", Name = "DataValue")]
            public IEntityTable<MembersAttribute> MembersAttribute
            {
                get { return (IEntityTable<MembersAttribute>)EntityProvider.GetTable(typeof(MembersAttribute), "MembersAttribute"); }
                private set { }
            }

            [Table(Name = "members", Alias = "M")]
            [Column(Member = "Id", Name = "Id", IsPrimaryKey = true, IsGenerated = false)]
            [Column(Member = "UserName", Name = "Username")]
            public IEntityTable<Members> Members
            {
                get
                {
                    return (IEntityTable<Members>)EntityProvider.GetTable(typeof(Members), "Members");
                }
                private set { }
            }
        }
        */
    }
}
